const express = require('express')

const { v4: uuidv4 } = require('uuid');
const router = express.Router()
const db = require('../../config/db.js')
// 引入jwt token工具
const JwtUtil = require('../../utils/jwt');

const dayjs = require('dayjs');

//获取所有角色信息
router.get('/roleinfor', function (req, res) {
    // 前端接收参数
    let role = req.query.role;
    let searchKey = req.query.searchKey;
    let option = req.query.option;
    // 请求需要进行token校验 
    console.log("role", role);
    console.log("searchKey", searchKey);
    console.log("option", option);
    // console.log(req.query);
    // let token = req.headers.etoken;
    // let jwt = new JwtUtil(token)
    // let result = jwt.verifyToken()
    // console.log(result.data);
    // let id = result.data;
    let user_check_sql;
    if (role && !searchKey) {
        user_check_sql = 'select * from user where role="' + role + '"';
    } else if (role && searchKey) {
        user_check_sql = 'select * from user where role="' + role + '" and jobnumber="' + searchKey + '" or userName="' + searchKey + '"';
    }
    // 查询用户表
    // let user_check_sql = 'select * from user where role="' + role + '"';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            // let [{ userName, gender, jobnumber, phone, avatar }] = rows;
            // console.log(typeof avatar);
            let result = option ? rows.map((item) => {
                return {
                    userName: item.userName,
                    jobnumber: item.jobnumber
                }
            }) : rows.map((item) => {
                return {
                    phone: item.phone,
                    userName: item.userName,
                    role: item.role,
                    avatar: item.avatar,
                    gender: item.gender,
                    jobnumber: item.jobnumber
                }
            });
            res.send({
                data: {
                    code: 200,
                    msg: '查询成功',
                    success: true,
                    data: {
                        result
                    }
                }
            })
        }
    })
});

// 删除咨讯数据
router.delete('/delete', function (req, res) {
    console.log(req.query);
    let jobnumber = req.query.jobnumber;
    let id = req.query.id;
    let classifyId = req.query.classifyId;
    // console.log(jobnumber);
    // "delete from  数据表 where 字段 = '" + 值 + "'"
    // let sql;
    // if (id) {
    //     sql = 'delete from Departmentinfo where id = "' + id + '"';
    // }
    //  else if (jobnumber) {
    //     sql = 'delete from User where jobnumber = "' + jobnumber + '"';
    // }
    //  else if (classifyId) {
    //     sql = 'delete from Departmentclassify where id = "' + classifyId + '"';
    // }
    let sql = 'delete from Inquiries where id = "' + id + '"';
    db.query(sql, function (err) {
        if (err) {
            res.send({
                code: -1,
                msg: '删除失败',
                err,
            })
        } else {
            res.send({
                data: {
                    code: 200,
                    msg: '删除成功',
                    success: true,
                }
            })
        }
    })

})

// 增加咨讯信息
router.post('/inquiriesinfo', function (req, res) {
    // 请求需要进行token校验 
    console.log(req.body);
    let token = req.headers.etoken;
    let synopsis = req.body.synopsis;
    let content = req.body.content;
    let title = req.body.title;
    let jwt = new JwtUtil(token)
    let result = jwt.verifyToken();
    let time = dayjs().valueOf();
    let id = uuidv4() || Math.random().toString().slice(2);
    // 接收前端的参数
    if (result == 'err') {
        res.send({
            code: -1,
            msg: '登录已过期,请重新登录'
        })
    } else {
        // 接收前端的文件
        var user_update_sql = 'insert into Inquiries(synopsis,content,title,time,id) values(' + '"' + synopsis + '"' + ',' + '"' + content + '"' + ',' + '"' + title + '"' + ',' + '"' + time + '"' + ',' + '"' + id + '"' + ')';
        db.query(user_update_sql, function (err, rows) {
            if (err) {
                res.send({
                    code: -1,
                    msg: "添加失败"
                });
            } else {
                res.send({
                    data: {
                        code: 200,
                        msg: "添加成功",
                        success: true,
                    }
                });
            }
        });
    }
})

//获取所有咨讯
router.get('/inquiriesinfo', function (req, res) {
    // 前端接收参数
    let id = req.query.id;
    // 请求需要进行token校验 
    console.log("id", id);
    let user_check_sql;
    if (id) {
        user_check_sql = 'select * from inquiries where id = "' + id + '"';
    } else{
        user_check_sql = 'select * from inquiries';
    } 
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows
                    },
                },
                code: 200,
                msg: '获取成功',
                success: true,
            })
        }
    })
});

// 修改咨询数据
router.put('/inquiriesinfo', function (req, res) {
    // 请求需要进行token校验 
    console.log(req.headers);
    console.log(req.body);
    let token = req.headers.etoken;
    let title = req.body.title;
    let synopsis = req.body.synopsis;
    let content = req.body.content;
    let id = req.body.id;
    let jwt = new JwtUtil(token)
    let result = jwt.verifyToken()
    console.log(result.data);
    // 接收前端的参数
    if (result == 'err') {
        res.status(401).send({
            code: 3002,
            msg: '登录已过期,请重新登录'
        })
    } else {
        
        let user_check_sql = "select * from inquiries where id='" + id + "'";
        db.query(user_check_sql, (err, rows) => {
            if (err) {
                res.send({
                    code: -1,
                    msg: '查询失败',
                    err
                })
            } else {
                if (rows && rows.length != 0) {
                    var user_update_sql = "update inquiries set content = '" + content + "',synopsis = '" + synopsis + "',title = '" + title + "' where  id = '" + id + "'";
                    db.query(user_update_sql, function (err, rows) {
                        if (err) {
                            res.send({
                                code: -1,
                                msg: "设置失败",
                                err
                            });
                        } else {
                            res.send({
                                data: {
                                    code: 200,
                                    msg: "设置成功",
                                    success: true,
                                }
                            });
                        }
                    });
                }
            }
        })
    }
})

// 增加医院信息
router.post('/hospitalinfo', function (req, res) {
    // 请求需要进行token校验 
    console.log(req.body);
    let token = req.headers.etoken;
    let synopsis = req.body.synopsis;
    let name = req.body.name;
    let phone = req.body.phone;
    let scale = req.body.scale;
    let institution = req.body.institution;
    let img = req.body.img;
    let address = req.body.address;
    let jwt = new JwtUtil(token)
    let result = jwt.verifyToken();
    let id = uuidv4() || Math.random().toString().slice(2);
    // 接收前端的参数
    if (result == 'err') {
        res.send({
            code: -1,
            msg: '登录已过期,请重新登录'
        })
    } else {
        // 接收前端的文件
        var user_update_sql = 'insert into Hospital(synopsis,name,phone,scale,id,institution,img,address) values(' + '"' + synopsis + '"' + ',' + '"' + name + '"' + ',' + '"' + phone + '"' + ',' + '"' + scale + '"' + ',' + '"' + id + '"' + ',' + '"' + institution + '"' + ',' + '"' + img + '"' + ',' + '"' + address + '"' + ')';
        db.query(user_update_sql, function (err, rows) {
            if (err) {
                res.send({
                    code: -1,
                    msg: "添加失败"
                });
            } else {
                res.send({
                    data: {
                        code: 200,
                        msg: "添加成功",
                        success: true,
                    }
                });
            }
        });
    }
})
//获取医院信息
router.get('/hospitalinfo', function (req, res) {
    // 前端接收参数
    // let id = req.query.id;
    // 请求需要进行token校验 
    // console.log("id", id);
    let user_check_sql = 'select * from hospital';
     
    console.log(user_check_sql);
    // 查询用户表
    //  user_check_sql = 'select * from department';
    db.query(user_check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                msg: '查询失败'
            })
        } else {
            // console.log(rows);
            res.send({
                data: {
                    code: 200,
                    msg: '获取成功',
                    success: true,
                    data: {
                        result: rows
                    },
                },
                code: 200,
                msg: '获取成功',
                success: true,
            })
        }
    })
});

// 修改医院信息
router.put('/hospitalinfo', function (req, res) {
    // 请求需要进行token校验 
    console.log(req.headers);
    console.log(req.body);
    let token = req.headers.etoken;
    let name = req.body.name;
    let synopsis = req.body.synopsis;
    let phone = req.body.phone;
    let scale = req.body.scale;
    let institution = req.body.institution;
    let img = req.body.img;
    let address = req.body.address;
    let id = req.body.id;
    let jwt = new JwtUtil(token)
    let result = jwt.verifyToken()
    console.log(result.data);
    // 接收前端的参数
    if (result == 'err') {
        res.status(401).send({
            code: 3002,
            msg: '登录已过期,请重新登录'
        })
    } else {
        
        let user_check_sql = "select * from hospital where id='" + id + "'";
        db.query(user_check_sql, (err, rows) => {
            if (err) {
                res.send({
                    code: -1,
                    msg: '查询失败',
                    err
                })
            } else {
                if (rows && rows.length != 0) {
                    var user_update_sql = "update hospital set name = '" + name + "',scale = '" + scale + "',institution = '" + institution + "',img = '" + img + "',address = '" + address + "',synopsis = '" + synopsis + "',phone = '" + phone + "' where  id = '" + id + "'";
                    db.query(user_update_sql, function (err, rows) {
                        if (err) {
                            res.send({
                                code: -1,
                                msg: "设置失败",
                                err
                            });
                        } else {
                            res.send({
                                data: {
                                    code: 200,
                                    msg: "设置成功",
                                    success: true,
                                }
                            });
                        }
                    });
                }
            }
        })
    }
})

module.exports = router;